package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.ReportService;
import com.itheima.health.service.SetMealService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据报告控制器
 */
@RestController
@RequestMapping("/report")
@Slf4j
@Api(tags = "数据报告展示接口")
public class ReportController {

    @Autowired
    private SetMealService setMealService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private MemberService memberService;

    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {

        log.info("套餐占比饼状图");
        List<Map<String, Object>> list = orderService.findSetmealCount();
        List<String> setmealNames = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("setmealCount",list);
        for (Map<String, Object> map1 : list) {
            String name = (String) map1.get("name");
            setmealNames.add(name);
        }

        map.put("setmealNames",setmealNames);
        return new Result(true,MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
    }



    /**
     * 任务13  统计分析-统计会员数量折线图
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        List<String> months = new ArrayList<>();
        List<Integer> memberCount = null;
        //mouths怎么求出？
        Calendar calendar = Calendar.getInstance();//2022.12.7
        calendar.add(Calendar.MONTH, -12);//2021.12.7
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
        for (int i = 0; i < 12; i++) {//2021.12->2022.12
            String mouth = sdf.format(calendar.getTime());
            months.add(mouth);//添加到集合
            calendar.add(Calendar.MONTH,1);//月份加1
        }
        //memberCount怎么求出？
        memberCount = memberService.findCountMember(months);
        //封装到map中返回map
        Map<String, Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);
        return new Result(true,MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }
}
